package mapreduce.flowcount.task3;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //1:遍历集合,并将集合中的对应的四个字段累计
        Integer upPackSum = 0;
        Integer downPackSum = 0;
        Integer upFlowSum = 0;
        Integer downFlowSum = 0;

        for (FlowBean flowBean : values){
            upPackSum += flowBean.getUpPackCount();
            downPackSum += flowBean.getDownPackCount();
            upFlowSum += flowBean.getUpFlowCount();
            downFlowSum += flowBean.getDownFlowCount();
        }

        //2:创建FlowBean对象,并给对象赋值  V3
        FlowBean flowBean = new FlowBean(upPackSum, downPackSum, upFlowSum, downFlowSum);

        //3:将K3和V3下入上下文中
        context.write(key, flowBean);
    }
}
